package com.xiaolin.shiro.controller;

import com.xiaolin.shiro.model.Member;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.authc.credential.DefaultPasswordService;
import org.apache.shiro.authc.credential.PasswordMatcher;
import org.apache.shiro.authc.credential.PasswordService;
import org.apache.shiro.authz.AuthorizationException;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.apache.shiro.crypto.hash.DefaultHashService;
import org.apache.shiro.subject.Subject;
import org.apache.shiro.util.ByteSource;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @ClassName LoginController
 * @Description 登录拦截
 * @Detail detail
 * @Author MyPC
 * @Date 2020/10/11
 * @Version 1.0
 */
@Controller
public class LoginController {

    @RequestMapping("/to-login")
    public String login(){
        return "login";
    }

    @RequestMapping("/login")
    public String login(Member member, Model model){
        if(StringUtils.isEmpty(member.getUsername()) || StringUtils.isEmpty(member.getPassword())){
            return "login";
        }
        //获取当前用户
        Subject currentUser = SecurityUtils.getSubject();
        UsernamePasswordToken usernamePasswordToken = new UsernamePasswordToken(member.getUsername(), member.getPassword());
        try {
            currentUser.login(usernamePasswordToken);
        }catch (UnknownAccountException e) {
            model.addAttribute("error","用户名不存在！");
            return "error";
        } catch (AuthenticationException e) {
            model.addAttribute("error","账号或密码错误！");
            return "error";
        } catch (AuthorizationException e) {
            model.addAttribute("error","没有权限");
            return "error";
        }
        System.out.println(currentUser.getPrincipal().toString());
        model.addAttribute("username",currentUser.getPrincipal().toString());
        DefaultHashService hashService = new DefaultHashService();
        // 设置加密算法，默认是 SHA-512
        hashService.setHashAlgorithmName("SHA-512");
        // 设置私盐
        hashService.setPrivateSalt(ByteSource.Util.bytes("abc"));
        // 设置是否生成公盐
        hashService.setGeneratePublicSalt(true);
        return "index";
    }

    /**
     * 注销
     * @return
     */
    @RequestMapping("/logout")
    public String logout(){
        Subject currentUser = SecurityUtils.getSubject();
        currentUser.logout();
        return "logout";
    }

    /**
     * 按角色来控制权限,有包含指定的所有角色才能访问
     * @return
     */
    @RequiresRoles(value = {"common"})
    @GetMapping("/common")
    public String common() {
        return "common";
    }

    /**
     * 根据权限来控制,有包含指定的所有权限才能访问
     * @return
     */
    @RequiresPermissions({"select","add"})
    @GetMapping("/admin")
    public String admin() {
        return "admin";
    }
}
